// pages/activesUpload/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{ id: 0, value: "发布活动", isActive: true }],
    depArr: [],
    depAllInfo: [],
    applyStartTime: "报名开始时间",
    applyEndTime: "报名结束时间",
    activeStartTime: "活动开始时间",
    activeEndTime: "活动结束时间",
    depDes: "选择主办部门",
    depDes2: "",
    date1: "",
    date2: "",
    date3: "",
    date4: "",
    index: 0,
    // 被选中的图片路径 数组
    chooseImgs: [],
    tempFilePaths: [],
  },
  //接口返回的图片的路径数组
  UpLoadImgs: [],

  onLoad: function () {
    this.getDepList();
  },

  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
    });
  },
  bindDateChange: function (e) {
    this.setData({
      date1: e.detail.value,
    });
  },
  bindDateChange2: function (e) {
    this.setData({
      date2: e.detail.value,
    });
  },
  bindDateChange3: function (e) {
    this.setData({
      date3: e.detail.value,
    });
  },
  bindDateChange4: function (e) {
    this.setData({
      date4: e.detail.value,
    });
  },

  //部门数据获取
  async getDepList() {
    const res = await request({ url: "/faculty/findAllFaculty" });
    //console.log(111, res);
    let depList = res.data.data;
    let depNameArr = [];
    depList.map((v) => {
      depNameArr.push(v.faculty_name);
    });
    this.setData({
      depArr: depNameArr,
      depAllInfo: depList,
    });
  },

  //点击删除图片
  handleRemoveImg(e) {
    // 2 获取被点击的组件的索引
    const { index } = e.currentTarget.dataset;
    // 3 获取data中的图片数组
    let { chooseImgs } = this.data;
    // 4 删除元素
    chooseImgs.splice(index, 1);
    this.setData({
      chooseImgs,
    });
  },

  // 点击 “+” 选择图片
  handleChooseImg() {
    let that = this;
    // 2 调用小程序内置的选择图片api
    wx.chooseImage({
      // 同时选中的图片的数量
      count: 9,
      // 图片的格式  原图  压缩
      sizeType: ["original", "compressed"],
      // 图片的来源  相册  照相机
      sourceType: ["album", "camera"],

      success: (result) => {
        const tempFilePaths = result.tempFilePaths;
        console.log(444, result);
        this.setData({
          // 图片数组 进行拼接
          chooseImgs: [...this.data.chooseImgs, ...result.tempFilePaths],
          tempFilePaths: result.tempFilePaths,
        });
        wx.uploadFile({
          url: "http://172.17.26.211:3310/uploadFile",
          filePath: tempFilePaths[0],
          name: "file",
          formData: {
            user: "test",
          },
          success(result) {
            console.log(999, result);
            let url = JSON.parse(result.data);
            console.log(1000, url.data);
            console.log(3000, that.testValue);
            // that.setData({ UpLoadImgs: url.data });
            let imgUrl = url.data;
            that.UpLoadImgs.push(imgUrl);
            console.log(1100, that.UpLoadImgs);
          },
        });
      },
    });
  },

  // 提交按钮的点击
  async formSubmit(e) {
    let formData = e.detail.value;
    formData.active_main_picture = this.UpLoadImgs[0];
    const res = await request({
      url: "/activites/addCampusActivites",
      method: "post",
      data: formData,
    });
    wx.showLoading({
      title: "正在上传中",
      mask: true,
    });
    wx.navigateBack({
      delta: 1,
    });

    // 2 合法性的验证
    // if (!this.data.depDes.trim()) {
    //   // 不合法
    //   wx.showToast({
    //     title: "输入不合法",
    //     icon: "none",
    //     mask: true,
    //   });
    //   return;
    // }

    //重置页面
    this.setData({
      date1: "",
      date2: "",
      date3: "",
      date4: "",
    });
  },
});
